<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="LAFDC.DataAccess" %>
<%@ Import Namespace="LAFDC.Logging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="c#" runat="server">
    private string source = "RoutageSave.aspx";
    public string debug_data = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        FDCLogger.LogEvent("Sauvegarde du Routage en cours!" , source, 4);
        // Load the params into variables
        
        String [] c = new String[8];
 
        c[0] = Request.Params.Get("prev_id");
        c[1] = Request.Params.Get("asso");
        c[2] = Request.Params.Get("code");
        c[3] = Request.Params.Get("destinataire");
        c[4] = Request.Params.Get("address_1");
        c[5] = Request.Params.Get("address_2");
        c[6] = Request.Params.Get("ville_cp");
        c[7] = Request.Params.Get("quantite");

        debug_data = "<p>" + this.Request.QueryString.ToString() + "</p>";
        int i = 0;
        foreach (String entry in c)
        {
            debug_data += "<p>foreach output: c" + i.ToString() + "=" + entry + "</p>";
            i++;
        }
        
        FDCLogger.LogEvent("Preparation de la commande de mise a jour SQL" , source, 4);
        
        int return_code = SaveData(c);

        FDCLogger.LogEvent("Resultat de la commande de mise a jour SQL=" + return_code.ToString() , source, 4);

        if (return_code == 1){
            Response.Redirect("RoutageView.aspx?adh=" + c[0]);
        }
    }
    
    private int SaveData(string[] data)
    {
        int result=29873425;

        ConnectionManager cn = new ConnectionManager();
        SqlConnection conn = cn.GetSqlConnection();
        SqlCommand tsql = conn.CreateCommand();
        
        tsql.CommandType = CommandType.Text;
        
        tsql.CommandText = @"set nocount off; update routage
        set asso   	= '" + data[1] + @"',
	    code 		= '" + data[2] + @"',
	    destinataire= '" + data[3] + @"',
	    address_1	= '" + data[4] + @"',
        address_2	= '" + data[5] + @"',
	    ville_cp	    = '" + data[6] + @"',
	    quantite	= '" + data[7] + @"'
    where prev_id	= '" + data[0] + "'";

        FDCLogger.LogEvent("Preparing to update routage with: " + tsql.CommandText, source, 4);

        conn.Open();
        result = tsql.ExecuteNonQuery();
        conn.Close();
        
        return result;
    }
</script>
<%Response.Write(debug_data);%>
